package com.ej.busi.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ej.busi.domain.EjCatalogue;
import com.ej.busi.domain.EjCourse;
import com.ej.busi.domain.EjUserCourse;
import com.ej.busi.dto.catalogue.EjCatalogueDto;
import com.ej.busi.mapper.EjCatalogueMapper;
import com.ej.busi.service.IEjCatalogueService;
import com.ej.busi.service.IEjCourseService;
import com.ej.busi.service.IEjUserCourseService;
import com.ej.common.exception.base.BaseException;
import com.ej.common.utils.SecurityUtils;
import com.ej.common.utils.StringUtils;
import com.ej.common.utils.bean.BeanUtils;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ej
 * @date 2025-08-11
 */
@Service
public class EjCatalogueServiceImpl extends ServiceImpl<EjCatalogueMapper, EjCatalogue> implements IEjCatalogueService {

    @Autowired
    private IEjCourseService courseService;

    @Autowired
    private IEjUserCourseService userCourseService;

    @Override
    public List<EjCatalogueDto> userCatalogueList(String courseName) {

        List<EjCatalogueDto> result = Lists.newArrayList();
        /**
         * 获取用户id
         */
        Long userId = SecurityUtils.getLoginUser().getUserId();
        if (userId == null){
            throw new BaseException("请先登录!");
        }

        List<EjCatalogue> list = this.list(Wrappers.<EjCatalogue>lambdaQuery().eq(EjCatalogue::getUserId, userId));
        if (CollectionUtils.isEmpty(list)){
            return result;
        }
        LambdaQueryWrapper<EjCourse> query = Wrappers.<EjCourse>lambdaQuery().in(EjCourse::getId, list.stream().map(EjCatalogue::getCourseId).collect(Collectors.toList()));
        if (StringUtils.isNotEmpty(courseName)){
            query.like(EjCourse :: getName, courseName);
        }

        // 课程表
        List<EjCourse> courseList = courseService.list(query);
        Map<Long, EjCourse> courseMap = courseList.stream().collect(Collectors.toMap(entity -> entity.getId(), Function.identity()));

        // 用户和课程关联表
        List<EjUserCourse> allUserCourses = userCourseService.list(Wrappers.<EjUserCourse>lambdaQuery().eq(EjUserCourse::getUserId, userId)
                .in(EjUserCourse::getCourseId, courseList.stream().map(s -> s.getId()).collect(Collectors.toList())));
        Map<Long, List<EjUserCourse>> userCoursesMap = allUserCourses.stream().collect(Collectors.groupingBy(EjUserCourse::getCourseId));

        list.forEach( ejCatalogue -> {
            EjCatalogueDto ejCatalogueDto = new EjCatalogueDto();
            BeanUtils.copyBeanProp(ejCatalogueDto, ejCatalogue);

            // 获取课程
            EjCourse ejCourse = courseMap.get(ejCatalogue.getCourseId());
            ejCatalogueDto.setCourseName(ejCourse.getName());
            ejCatalogueDto.setPreviewPic1(ejCourse.getPreviewPic1());

            // 课程状态
            List<EjUserCourse> userCourses = userCoursesMap.get(ejCatalogue.getCourseId());
            EjUserCourse ejUserCourse = userCourses.stream().sorted(Comparator.comparing(EjUserCourse::getEffectiveTime)).collect(Collectors.toList()).get(0);
            if (ejUserCourse.getEffectiveTime().after(new Date())){
                ejCatalogueDto.setCourseStatus("0");
            }else {
                ejCatalogueDto.setCourseStatus("1");
            }

            result.add(ejCatalogueDto);
        });

        return result;
    }
}
